package qt.controller;

import com.sun.org.apache.xpath.internal.SourceTree;
import ht.domain.*;
import ht.ser.IndexSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import qt.domain.*;
import qt.service.QTIndexService;
import qt.service.StationSer;
import utils.StcdUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanlin on 2017/10/21.
 */
@Controller
@RequestMapping("/qtIndex")
public class QTIndexController {

    @Resource
    private QTIndexService qTIndexServiceImpl;
    @Resource(name = "indexSerImpl")
    private IndexSer indexSer;
    @Resource
    private StationSer stationSerImpl;

    @RequestMapping("/stationInfo")
    public String stationInfo(HttpServletRequest request, String id) {
        request.getSession().setAttribute("sel_nodeId", id);
        if(!id.contains("g")){
            request.getSession().setAttribute("sel_addvcd",id);
        }
        return "qt/jsp/station_info";
    }
    @RequestMapping("/lskzmInfo")
    public String lskzmInfo(HttpServletRequest request, String id) {
        request.getSession().setAttribute("sel_nodeId", id);
        if(!id.contains("g")){
            request.getSession().setAttribute("sel_addvcd",id);
        }
        return "qt/jsp/lskzm";
    }
    @RequestMapping("/sskzmInfo")
    public String sskzmInfo(HttpServletRequest request, String id) {
        request.getSession().setAttribute("sel_nodeId", id);
        if(!id.contains("g")){
            request.getSession().setAttribute("sel_addvcd",id);
        }
        return "qt/jsp/sskzm";
    }

    @RequestMapping("/lskzmdygx")
    public String toHistory() {
        return "qt/jsp/lskzm_index";
    }

    @RequestMapping("/sskzmdygx")
    public String toSskzmdy() {
        return "qt/jsp/sskzm_index";
    }

    @RequestMapping("/czglgx")
    public String toCzglgx() {
        return "qt/jsp/czglgx";
    }

    @RequestMapping("/map")
    public String map() {
        return "qt/jsp/map";
    }

    @ResponseBody
    @RequestMapping("/xzqhxxTree")
    public List<TreeChild> getXzqhxxTree() {
        List<TreeChild> children = qTIndexServiceImpl.getXzqhxxTree();
        return children;
    }

    @ResponseBody
    @RequestMapping("/gldwTree")
    public List<TreeChild> getGldwTree() {
        List<TreeChild> children = qTIndexServiceImpl.getGldwTree();
        return children;
    }

    @ResponseBody
    @RequestMapping("/xzqhName")
    public String getXzqhNameById(HttpServletRequest request) {
        String nodeId = (String) request.getSession().getAttribute("sel_nodeId");
        if (nodeId == null || nodeId.equals("") || nodeId.equals("00")) {
            return "宁波市";
        } else if (nodeId.contains("g")) {
            return qTIndexServiceImpl.getGldwName(nodeId.substring(1));
        } else {
            return qTIndexServiceImpl.getXzqhName(nodeId);
        }
    }

    @ResponseBody
    @RequestMapping("/hlxx/combo")
    public List<Combo> comboHlxx() {
        List<Hlxx> list = indexSer.getAllHlxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Hlxx x : list) {
            Combo c = new Combo(x.getRvnm(), x.getRvcd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/sxxx/combo")
    public List<Combo> comboSxxx() {
        List<Sxxx> list = indexSer.getAllSxxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Sxxx x : list) {
            Combo c = new Combo(x.getHnnm(), x.getHncd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/lyxx/combo")
    public List<Combo> comboLyxx() {
        List<Lyxx> list = indexSer.getAllLyxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Lyxx x : list) {
            Combo c = new Combo(x.getBsnm(), x.getBscd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/czdjxx/combo")
    public List<Combo> comboCzdjxx() {
        List<Czdjxx> list = indexSer.getAllCzdjxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Czdjxx x : list) {
            Combo c = new Combo(x.getStgrd(), x.getStgrdcd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/czlxxx/combo")
    public List<Combo> comboCzlxxx() {
        List<Czlxxx> list = indexSer.getAllCzlxxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Czlxxx x : list) {
            Combo c = new Combo(x.getSttp(), x.getSttpcd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/czflxx/combo")
    public List<Combo> comboCzflxx() {
        List<Czflxx> list = indexSer.getAllCzflxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Czflxx x : list) {
            Combo c = new Combo(x.getStcl(), x.getStclcd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/gldw/combo")
    public List<Combo> comboGldw() {
        List<Gldw> list = indexSer.getAllGldw();
        List<Combo> llist = new ArrayList<Combo>();
        for (Gldw x : list) {
            Combo c = new Combo(x.getAdmag(), x.getAdmagcd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/ssxmxx/combo")
    public List<Combo> comboSsxmxx() {
        List<Ssxmxx> list = indexSer.getAllSsxmxx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Ssxmxx x : list) {
            Combo c = new Combo(x.getObitnm(), x.getObitcd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/glgx/combo")
    public List<Combo> comboGlgx() {
        List<Relation> list = indexSer.getAllRelation();
        List<Combo> llist = new ArrayList<Combo>();
        for (Relation x : list) {
            Combo c = new Combo(x.getRelanm(), x.getRela());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/ssxmxx/checkbox")
    public List<CheckBox> CheckBoxSsxmxx() {
        List<CheckBox> list = stationSerImpl.getScxmCheckBoxForAdd();
        return list;
    }

    @ResponseBody
    @RequestMapping("/ssxmxx/checkboxUpd")
    public List<CheckBox> CheckBoxSsxmxxForUpdate(String stcd) {
        List<CheckBox> list = stationSerImpl.getScxmCheckBoxForUpdate(stcd);
        return list;
    }

    @ResponseBody
    @RequestMapping("/station/add")
    public Response addStation(Station st) {
        if (stationSerImpl.addStation(st) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/station/update")
    public Response updateStation(Station st) {
//        System.out.println(st);
        if (stationSerImpl.upptStation(st) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/station/table")
    public DataTable<StationShow> getStationForTable(HttpServletRequest request, int draw, int start, int length) {
        String nodeId = (String) request.getSession().getAttribute("sel_nodeId");
        String srh = request.getParameter("search[value]");
        int limit = length;
        if (srh == null || srh.equals("")) {
            //无搜索内容
            if (nodeId == null || nodeId.equals("") || nodeId.equals("00")) {
                return stationSerImpl.getDataTableStation(draw, start, limit);
            } else if (nodeId.contains("g")) {
                String admagcd = nodeId.substring(1);
                return stationSerImpl.getDataTableStationByGldw(draw, start, limit, admagcd);
            } else {
                return stationSerImpl.getDataTableStationByAddvcd(draw, start, limit, nodeId);
            }
        } else {
        //有搜索内容
        //return stationSerImpl.getDataTableStationByNmOrId(0, 0, 30, srh);
        return stationSerImpl.getDataTableStationByNmOrId(draw, start, limit, srh);
    }
    }

    @ResponseBody
    @RequestMapping("/station/getById")
    public StationShow getStationById(String id) {
        return stationSerImpl.getStation(id);
    }

    @ResponseBody
    @RequestMapping("/station/getCountByAccvcd")
    public String getStationCount(HttpServletRequest request) {
        String addvcd = (String) request.getSession().getAttribute("sel_addvcd");
        String count = String.valueOf(stationSerImpl.getLastStationCountByAddvcd(addvcd) + 1);
        if (count.length() == 1) {
            count = "000" + count;
        } else if (count.length() == 2) {
            count = "00" + count;
        } else if (count.length() == 3) {
            count = "0" + count;
        }
        return "01" + addvcd + "." + count + ".00";
    }

    @ResponseBody
    @RequestMapping("/station/getNewStcd")
    public String getStationStcd(String addvcd) {
        String count = String.valueOf(stationSerImpl.getLastStationCountByAddvcd(addvcd) + 1);
        if (count.length() == 1) {
            count = "000" + count;
        } else if (count.length() == 2) {
            count = "00" + count;
        } else if (count.length() == 3) {
            count = "0" + count;
        }
        return "01" + addvcd + "." + count + ".00";
    }

    @ResponseBody
    @RequestMapping("/station/getByNmOrId")
    public DataTable<StationShow> getDataTableStationByNmOrId(String srh) throws UnsupportedEncodingException {
        String search_val = java.net.URLDecoder.decode(srh, "utf-8");
        //System.out.println(search_val);
        return stationSerImpl.getDataTableStationByNmOrId(0, 0, 30, search_val);
    }

    @ResponseBody
    @RequestMapping("/lskzmdygx/all")
    public DataTable<LskzmdygxShow> getAllLskzmdygx(HttpServletRequest request,int draw, int start, int length) {
        String nodeId = (String) request.getSession().getAttribute("sel_nodeId");
        //System.out.println(nodeId);
        String srh = request.getParameter("search[value]");
        int limit = length;
        if (srh == null || srh.equals("")) {
            //无搜索内容
            if (nodeId == null || nodeId.equals("") || nodeId.equals("00")) {
                return stationSerImpl.getDataTableLskzmdygx(draw, start, limit);
            }
           else if (nodeId.contains("g")) {
                String admagcd = nodeId.substring(1);
                return stationSerImpl.getDataTableLskzmdygxByGldw(draw, start, limit, admagcd);
            } else {
                return stationSerImpl.getDataTableLskzmdygxByAddvcd(draw, start, limit, nodeId);
            }
        } else {
            //有搜索内容
            //return stationSerImpl.getDataTableLskzmdygxByNmOrId(0, 0, 30, srh);
            return stationSerImpl.getDataTableLskzmdygxByNmOrId(draw, start, limit, srh);
        }

    }

    @ResponseBody
    @RequestMapping("/lskzmdygx/getByNmOrId")
    public DataTable<LskzmdygxShow> getLskzmdygxByNmOrId(int draw, int start, int limit, String nmOrId) {
        return stationSerImpl.getDataTableLskzmdygxByNmOrId(draw, start, limit, nmOrId);
    }

    @ResponseBody
    @RequestMapping("/lskzmdygx/add")
    public Response addLskzmdygx(Lskzmdygx lskzmdygx) {
        if (stationSerImpl.addLskzmdygx(lskzmdygx) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/lskzmdygx/update")
    public Response udpLskzmdygx(Lskzmdygx lskzmdygx) {
        if (stationSerImpl.upptLskzmdygx(lskzmdygx) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/lskzmdygx/getById")
    public LskzmdygxShow getlskzmdygxById(String id) {
        return stationSerImpl.getLskzmadygxById(id);
    }

    @ResponseBody
    @RequestMapping("/stationIdAndNm/combo")
    public List<Combo> comboCzglgx() {
        List<Station> list = stationSerImpl.getAllStationIdAndNm();
        List<Combo> llist = new ArrayList<Combo>();
        for (Station x : list) {
            Combo c = new Combo(x.getStnm(), x.getStcd());
            llist.add(c);
        }
        return llist;
    }

    //分断面
    @ResponseBody
    @RequestMapping("/fdm/showAllFdm")
    public Page<Fdm> getAllFdmByStcd(String stcd) {
        return stationSerImpl.getAllFdm(stcd);
    }

    @ResponseBody
    @RequestMapping("/fdm/add")
    public Response add(Fdm h,String stcd){
        //h.setFdmid(StcdUtils.getPureStcd(stcd)+"."+h.getFdmid());
        if (stationSerImpl.addFdm(h,stcd)>0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/fdm/upd")
    public Response upptFdm(Fdm h,String stcd){
        if (stationSerImpl.upptFdm(h,stcd)>0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }
    @ResponseBody
    @RequestMapping("/fdm/delete")
    public Response deleFdm(String fdmid,String stcd){
        if (stationSerImpl.deleFdm(StcdUtils.getPureStcd(stcd)+"."+fdmid)>0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/fdm/check")
    public Response check(String fdmid,String stcd){
        if (stationSerImpl.checkFdm(fdmid, stcd)>0) {

            return Response.FAIL;
        } else {
            return Response.TRUE;
        }
    }


    @ResponseBody
    @RequestMapping("/czglgx/all")
    public DataTable<CzglgxShow> getAllCzglgx(HttpServletRequest request,int draw, int start, int length) {
        String srh = request.getParameter("search[value]");
        int limit = length;
        if (srh == null || srh.equals("")) {
            return stationSerImpl.getDataTableCzglgx(draw, start, limit);
        }else{
            //return stationSerImpl.getDataTableCzglgxByNmOrId(0, 0, 30, srh);
            return stationSerImpl.getDataTableCzglgxByNmOrId(draw, start, limit, srh);
        }

    }

    @ResponseBody
    @RequestMapping("/czglgx/add")
    public Response addCzglgx(Czglgx czglgx) {
        if (stationSerImpl.addCzglgx(czglgx) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/czglgx/update")
    public Response udpCzglgx(Czglgx czglgx) {
        if (stationSerImpl.upptCzglgx(czglgx) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/czglgx/delete")
    public Response delCzglgx(String stcd,String pstcd) {
        if (stationSerImpl.delCzglgx(stcd,pstcd) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/czglgx/showall")
    public Page<CzglgxShow> getAllCzglgx(String stcd) {
        return stationSerImpl.getAllCzglgx(stcd);
    }

    @ResponseBody
    @RequestMapping("/czglgx/check")
    public Response checkCzglgxSame(String stcd,String pstcd) {
        if (stationSerImpl.getCzglgxSame(stcd,pstcd) > 0) {
            //存在
            return Response.FAIL;
        } else {
            return Response.TRUE;
        }
    }

    @ResponseBody
    @RequestMapping("/czglgx/getById")
    public CzglgxShow getCzglgxById(String id,String pstcd) {
        return stationSerImpl.getCzglgxById(id,pstcd);
    }

    @ResponseBody
    @RequestMapping("/sskzmdygx/all")
    public DataTable<SskzmdygxShow> getAllSskzmdygx(HttpServletRequest request, int draw, int start, int length) {
        String srh = request.getParameter("search[value]");
        String nodeId = (String) request.getSession().getAttribute("sel_nodeId");
        int limit = length;
        //String addvcd = (String) request.getSession().getAttribute("ssk_xzqh");
        if (srh == null || srh.equals("")) {
            //无搜索内容
            if (nodeId == null || nodeId.equals("") || nodeId.equals("00")) {
                return stationSerImpl.getDataTableSskzmdygx(draw, start, limit);
            }
            else if (nodeId.contains("g")) {
                String admagcd = nodeId.substring(1);
                return stationSerImpl.getDataTableSskzmdygxByGldw(draw, start, limit, admagcd);
            } else {
                return stationSerImpl.getDataTableSskzmdygxByAddvcd(draw, start, limit, nodeId);
            }
        }else{
            //return stationSerImpl.getDataTableSskzmdygxByNm(0, 0, 30, srh);
            return stationSerImpl.getDataTableSskzmdygxByNm(draw, start, limit, srh);
        }
    }

    @ResponseBody
    @RequestMapping("/sskzmdygx/showall")
    public Page<SskzmdygxShow> getAllSskzmdygx(String stcd) {
        return stationSerImpl.getAllSskzm(stcd);
    }


    @ResponseBody
    @RequestMapping("/sskzmdygx/xzqhSelect")
    public void setSskzmdygxXzqh(HttpServletRequest request, String addvcd) {
        request.getSession().setAttribute("ssk_xzqh", addvcd);
    }


    @ResponseBody
    @RequestMapping("/sskzmdygx/add")
    public Response addSskzmdygx(Sskzmdygx s) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println(sdf.format(s.getFrdt()));
        stationSerImpl.getSame(s.getSscd());
        if (stationSerImpl.addSskzmdygx(s) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/sskzmdygx/check")
    public Response checkSame(String sscd) {
        if (stationSerImpl.getSame(sscd) > 0) {
            //确实不同
            return Response.FAIL;
        } else {
            return Response.TRUE;
        }
    }

    @ResponseBody
    @RequestMapping("/sskzmdygx/update")
    public Response udpSskzmdygx(Sskzmdygx s) {
        if (stationSerImpl.upptSskzmdygx(s) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/sskzmdygx/getById")
    public SskzmdygxShow getSskzmdygxById(String stcd, String tycd, String obitcd) {
        return stationSerImpl.getSskzmdygxById(stcd, tycd, obitcd);
    }

    @ResponseBody
    @RequestMapping("/sskzmdygx/delete")
    public Response delSskzmdygx(String stcd, String tycd, String obitcd) {
        if (stationSerImpl.deleSskzmdygx(stcd, tycd, obitcd) > 0) {
            return Response.TRUE;
        } else {
            return Response.FAIL;
        }
    }

    @ResponseBody
    @RequestMapping("/mlx/combo")
    public List<Combo> comboMlx() {
        List<Mlx> list = indexSer.getAllMlx();
        List<Combo> llist = new ArrayList<Combo>();
        for (Mlx m : list) {
            Combo c = new Combo(m.getTynm(), m.getTycd());
            llist.add(c);
        }
        return llist;
    }

    @ResponseBody
    @RequestMapping("/map/data")
    public List<Marker> getMarkers(String iids) {
        ArrayList<Marker> markers = new ArrayList<Marker>();
        if (iids.startsWith("all")) {
            String[] iid_array = iids.split("/");
            String id = iid_array[1];
            if(id.startsWith("g")){
                //表示为管理单位划分
                if (id.equals("gldw")||id.equals("00")) {
                    //全部的测站marker
                    List<Station> allStation = stationSerImpl.getAllStation();
                    for (Station s : allStation) {
                        markers.add(new Marker(s));
                    }
                } else {
                    List<Station> allStation = stationSerImpl.getStationByGldw(id.substring(1));
                    for (Station s : allStation) {
                        markers.add(new Marker(s));
                    }
                }
            }else{
                //表示为行政区划划分
                if (id.equals("xzqh")||id.equals("00")) {
                    //全部的测站marker
                    List<Station> allStation = stationSerImpl.getAllStation();
                    for (Station s : allStation) {
                        markers.add(new Marker(s));
                    }
                } else {
                    List<Station> allStation = stationSerImpl.getStationByAddvcd(id);
                    for (Station s : allStation) {
                        markers.add(new Marker(s));
                    }
                }
            }

        } else {
            //部分测站marker iid之间用／分割
            String[] iid_array = iids.split("/");
            for (String iid : iid_array) {
//                System.out.println(iid);
                StationShow station = stationSerImpl.getStation(iid);
                if (station != null) {
                    markers.add(new Marker(station));
                }
            }
        }
        return markers;
    }

}